package cn.zsb.dao;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @author oldliu
 * @since 1.0
 */
@Service
@Aspect
public class UserLanjie {
    @Around("within(cn.zsb.dao..*)")
    public Object lan(ProceedingJoinPoint jp) throws Throwable {
        System.out.println("方法：" + jp.getSignature().getName() + "执行于 " + new Date());
        System.out.println("目标对象：" + jp.getTarget());
        System.out.println("参数：" + Arrays.toString(jp.getArgs()));
        Object rtn = jp.proceed();//执行业务的方法
        System.out.println("方法:" + jp.getSignature().getName() + "执行结束于" + new Date() + "\n返回值是" + rtn);
        return rtn;
    }

    //@Around("within(cn.kgc83.dao..*)")
    //@AfterThrowing
    @Around("execution(* cn.zsb.dao..*.add(..))")
    public Object lando(ProceedingJoinPoint jp) throws Throwable {
        if(jp.getTarget() instanceof UserDao ==false){
            return jp.proceed();
        }
//
//       if(jp.getTarget().getClass()!=UserDao.class){
//
//       }
        if(jp.getSignature().getName().equals("add")){
            User user=(User)(jp.getArgs()[0]);
            if(user.getTele().length()!=11||!user.getTele().startsWith("1")){
                throw  new IllegalArgumentException("手机号错误");
                //return 0;
            }
        }
        Object rtn = jp.proceed();//执行业务的方法
        System.out.println("方法:" + jp.getSignature().getName() + "执行结束于" + new Date() + "\n返回值是" + rtn);
        return rtn;
    }


    @Around("within(cn.zsb.dao..*)")
    public Object lanFind(ProceedingJoinPoint jp) throws Throwable {
        if(jp.getTarget().getClass()!=UserDao.class ||jp.getSignature().getName().equals("search")==false){
            return jp.proceed();
        }
        Object rtn = jp.proceed();//执行业务的方法
        System.out.println("方法:" + jp.getSignature().getName() + "执行结束于" + new Date() + "\n返回值是" + rtn);
        List<User> list= (List<User>) rtn;
        list.removeIf(x->x.getName().equals("admin"));
        return list;
    }
}
